﻿Imports SchoolClick.Security.Providers
Imports SchoolClick.Filters
Imports System.Net

Namespace SchoolClick.Controllers
    Public Class AccountsController
        Inherits System.Web.Mvc.Controller

#Region "INSTANCIAS PARA EL MANEJO DE ROLES Y MEMBRESIAS DE USUARIOS"
        Public Property MembershipService As SchoolClickMemberShipProvider
        Public Property AuthorizationRolService As SchoolClickRoleProvider

        Protected Overrides Sub Initialize(requestContext As RequestContext)
            If (MembershipService Is Nothing) Then
                MembershipService = New SchoolClickMemberShipProvider
            End If

            If AuthorizationRolService Is Nothing Then
                AuthorizationRolService = New SchoolClickRoleProvider
            End If

            MyBase.Initialize(requestContext)
        End Sub
#End Region

        <HttpGet()> _
        <JFAllowAnonymous()> _
        Function LogOn() As ActionResult
            Return View()
        End Function

        <HttpPost()> _
        <JFValidarModel(), JFAllowAnonymous()> _
        <JFHandleExceptionMessage(Order:=1)> _
        Function LogOn(model As LoginModel, returnUrl As String) As JsonResult
            'Comprobamos que el Usuario y Contraseña sean validos
            If MembershipService.ValidateUser(model.UserName, model.Password) Then
                FormsAuthentication.SetAuthCookie(model.UserName, False)
                Return Json(New With {.success = True,
                                      .redirectURL = If(Url.IsLocalUrl(returnUrl), returnUrl, "/")})
            Else
                Response.TrySkipIisCustomErrors = True
                Response.StatusCode = HttpStatusCode.InternalServerError
                Return Json(New With {.notify = New JFNotifySystemMessage("El nombre de usuario o contraseña ingresadas son incorrectas",
                                                                       titulo:="Inicio de Sesión Fallido",
                                                                       permanente:=False,
                                                                       tiempo:=5000)}, JsonRequestBehavior.AllowGet)
            End If
        End Function

        <HttpPost()> _
        <Authorize(Roles:="ERA, ALU, PRF")> _
        <JFHandleExceptionMessage(Order:=1)> _
        <JFValidarModel()> _
        Public Function _op_change_password_user(model As ChangePasswordModel) As JsonResult
            Dim db As New SchoolClickDAL
            MembershipService.ChangePassword(User.Identity.Name, model.OLD_Password, model.NEW_Password)
            Return Json(New With {.success = True,
                                  .notify = New JFNotifySystemMessage("Su contraseña ha sido cambiada correctamente en el sistema.",
                                                                       titulo:="¡La contraseña se cambió correctamente!",
                                                                       permanente:=False,
                                                                       icono:=JFNotifySystemIcon.Update)})
        End Function

        Function NoAutorizado() As ActionResult
            Return View()
        End Function
    End Class
End Namespace
